package com.example.li.myapplication;

import android.media.Image;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView lv;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        intoView();

    }

    private void intoView() {
        lv = findViewById(R.id.lv);
        img = findViewById(R.id.img);
        img.setOnClickListener(this);
//        MyAdapter myAdapter = new MyAdapter();
//        lv.setAdapter(myAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img:
                PopupMenu popupMenu = new PopupMenu(this,img);
                getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.a:
                                    lisetnt();
                                break;
                            case R.id.b:

                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
                break;
        }
    }

    private void lisetnt() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view1 = View.inflate(this, R.layout.popuowindow, null);
        final EditText et = view1.findViewById(R.id.et);
        Button bu = view1.findViewById(R.id.btn);
        builder
                .setTitle("添加好友")
                .setIcon(R.mipmap.ic_launcher)
                .setView(view1)
                .create();
        final AlertDialog show = builder.show();
        bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = et.getText().toString();
                try {
                    EMClient.getInstance().contactManager().addContact(id, "账号查找");
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
                show.dismiss();
            }
        });
    }

    class MyAdapter extends BaseAdapter{


        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }
}
